# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import openpyxl as openpyxl
# useful for handling different item types with a single interface
from itemadapter import ItemAdapter


class Insta360Pipeline:
    # 创建excel表
    def __init__(self):
        self.container = []
        self.item = None
        self.wb = openpyxl.Workbook()
        self.ws = self.wb.active
        self.ws.title = 'insta360'
        self.ws.append(('标题', '地区', '播放状态', '类型', '照片', '链接',  '描述'))

    def open_spider(self, spider):
        print('ExcelPipeline:', '开始spider。。。。！')

    def close_spider(self, spider):
        self.wb.save('insta360.xlsx')
        print('ExcelPipeline:', '关闭spider。。。。！')

    def process_item(self, item, spider):
        title = item.get('title', '')
        region = item.get('region', '')
        status = item.get('status', '')
        picture = item.get('picture', '')
        url = item.get('url', '')
        type = item.get('type', '')
        introduce = item.get('introduce', '')
        self.container.append((title, region, status, type, picture, url, introduce))

        if len(self.container) == 100:
            self._write_Excel_()
            self.container.clear()
        return item

    def _write_Excel_(self):
        for self.item in self.container:
            print('container:', self.item)
            self.ws.append((self.item['title'], self.item['region'], self.item['status'], self.item['type'], self.item['picture'], self.item['url'], self.item['introduce']))
